home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2222 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Returning a variable from system() function
  5. Date: 19 Jan 1996 17:27:37 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4dp5sp$c5d@umbc9.umbc.edu>
  8. References: <295336694wnr@iiga.demon.co.uk>
  9. NNTP-Posting-Host: umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Pete Ryan  <pete@iiga.demon.co.uk> wrote:
  13. |>     I`ve just been experimenting with C on UNIX and have come across a 
  14. |> problem!.  There is a UNIX command called `find / -name gwire -print` 
  15. |> which scans the UNIX drive for files/directories containing `gwire`.  
  16. |> Anyway what I want to do is the following...
  17. |> 
  18. |> #include <stdio.h>
  19. |> #include <stdlib.h>
  20. |> etc
  21.  
  22. That 'etc' keyword gives me a syntax error, but you definitely need more
  23. includes even for this program since chdir() is a non-ANSI function.
  24.  
  25. |> void main()
  26.  
  27. You misspelled 'int main (void)'.
  28.  
  29. |> {
  30. |>     char    tmp[];
  31.  
  32. A non-determined size array?
  33.  
  34. |>     chdir("/usr2/willesden");    
  35.  
  36. See above...
  37.  
  38. |>     */ offending code below ;( */
  39. |>     tmp = system("find . -name gwire");
  40. |> 
  41. |>     printf("%s",tmp");
  42.  
  43.  
  44. The problem is system() returns an int. If you want some kind of result
  45. then either use redirection to a file and then read that file or consult
  46. your reference manuals or an appropriate UNIX newsgroup for non-portable
  47. solutions.
  48.  
  49. Oh and don't forget:
  50.  
  51. return (0);
  52. |> }
  53. |> 
  54. |> Therefore if I run `find . -name gwire -print` it returns all the 
  55. |> directories containing `gwire`.  However the code above does not 
  56. |> work!!. Is it because `= system` returns an integer and not strings???
  57.  
  58. You mean you know the problem and still asked? Yes, system() returns an
  59. int so assigning it to a string, which has no declared length, is
  60. going to give you problems to say the least.
  61. -- 
  62. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  63.  
  64. Jonas J. Schlein  (schlein@gl.umbc.edu)
  65.